package com.gem.sms.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gem.sms.entity.Student;
import com.gem.sms.service.StudentService;
import com.gem.sms.util.BeanFactory;

@WebServlet(name = "StudentServlet", urlPatterns = "/stu/*", loadOnStartup = 1)
public class StudentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		StudentService ss = (StudentService) BeanFactory.getBean("studentService");
		String base = request.getContextPath();
		String pathInfo = request.getPathInfo();
		String nextPage = "";
		boolean isRedirect = false;
		if ("/index".equals(pathInfo)) {
			nextPage = "login";
		} else if ("/login".equals(pathInfo)) {
			//获取前端发送的参数
			String sno = request.getParameter("stuno");
			String pwd = request.getParameter("password");
			Student stu = ss.canLogin(sno, pwd);
			PrintWriter out = response.getWriter();
			HttpSession session2 = request.getSession();
			//获取系统验证码
			String sCode = (String) session2.getAttribute("checkcode");
			//获取用户输入的验证码
			String uCode = request.getParameter("code");
			if (sCode.equalsIgnoreCase(uCode)) {
				if (stu != null) {

					String rememberMe = request.getParameter("rememberMe");
					if ("1".equals(rememberMe)) {
						//说明要使用记住我功能
						Cookie cookie = new Cookie("userInfo", sno + ":" + pwd);
						cookie.setMaxAge(7 * 24 * 60 * 60);
						response.addCookie(cookie);
					} else {
						//说明不要使用Cookie记录用户登录信息
						Cookie[] cookies = request.getCookies();
						Cookie cookie = getCookie(cookies, "userInfo");
						if (cookie != null) {
							cookie.setMaxAge(0);
							response.addCookie(cookie);
						}
					}
					//利用Session,保存用户登录信息
					HttpSession session = request.getSession();
					session.setAttribute("stu", stu);
					//				nextPage = "/permission/stuInfo/list";
					//				isRedirect = true;
					out.print("1");
				} else {
					//				nextPage = "/stu/index?info=1";
					//				isRedirect = true;
					out.print("0");
				}
			} else {
				out.print("2");//验证码错误
			}

			return;
		} else if ("/logout".equals(pathInfo)) {
			//说明要安全退出
			//1.销毁Session
			HttpSession session = request.getSession();
			session.invalidate();
			//2.跳转登录页面
			nextPage = "/stu/index";
			isRedirect = true;
		}

		if (isRedirect) {
			response.sendRedirect(base + nextPage);
		} else {
			request.getRequestDispatcher("/WEB-INF/pages/" + nextPage + ".jsp").forward(request, response);
		}

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	private Cookie getCookie(Cookie[] cookies, String name) {
		Cookie cookie = null;
		if (cookies != null && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (name.equals(c.getName())) {
					cookie = c;
					break;
				}
			}
		}
		return cookie;
	}
}
